package com.alipay.android.zoloz.face.ui;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.Toast;

import com.alipay.android.zoloz.R;
import com.alipay.mobile.security.zim.api.ZIMCallback;
import com.alipay.mobile.security.zim.api.ZIMFacade;
import com.alipay.mobile.security.zim.api.ZIMFacadeBuilder;
import com.alipay.mobile.security.zim.api.ZIMResponse;
import com.maotai.thread.MyThread;


import org.json.JSONException;
import org.json.JSONObject;


/**
 * @author xinwen
 */
public class ZolozFaceTestActivity extends Activity implements OnClickListener, ZIMCallback {

    public static final String TAG = "ZolozFaceTestActivity";
    public static final String NOT_SAME_PERSON = "NOT_SAME_PERSON";
    public static final String UNABLE_GET_IMAGE = "UNABLE_GET_IMAGE";

    String zimId;
    ZIMFacade zimFacade;

    EditText etZimid;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.zoloz_demo_test_main);

        findViewById(R.id.startCloudZim).setOnClickListener(this);
        etZimid = (EditText) findViewById(R.id.etZimid);
        findViewById(R.id.mybutton).setOnClickListener(this);
        ZIMFacade.install(this);//提前初始化
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.mybutton:
                getMaotaiInfo();
                break;
            case R.id.startCloudZim:
                zimId = etZimid.getText().toString().trim();
                onRequestResult(zimId);
                break;
            default:
                break;
        }
    }

    /**
     * zoloz校验结果回调
     * @param response
     */
    @Override
    public void response(ZIMResponse response) {
        JSONObject result = new JSONObject();
        try {
            result.put("code", response.code + "");
            result.put("verifyCode", "");
            result.put("reason", response.reason);
            result.put("bizData", response.bizData);
            result.put("extInfo", response.extInfo);

            Log.d(TAG, result.toString());
        } catch (JSONException e) {
            Log.w(TAG, e);
        }

        String msg;
        switch (response.code) {
            //核身通过
            case 1000:
                msg = "恭喜您，识别成功！";
                break;
            //核身失败
            case 1001:
                msg = "请确保是" + "本人操作";
                break;
            //用户选择其他核身方式
            case 1002:
                msg = "选择其他核身方式";
                break;
            //用户取消核身
            case 1003:
                msg = "用户取消核身";
                break;
            //核身任务超时
            case 1004:
                msg = "超时了，再试一次吧~";
                break;
            //人脸SDK处理异常
            case 1006:
                msg = "人脸SDK处理异常";
                break;
            //核身任务超时
            case 2001:
                msg = "核身任务超时";
                break;
            //未知错误
            case 2002:
                msg = "未知错误";
                break;
            //网络异常
            case 2003:
                msg = "网络异常";
                break;
            //核身任务重复
            case 2004:
                msg = "核身任务重复";
                break;
            //核身任务超限
            case 2005:
                msg = "刷脸多次失败，请3小时后再试";
                break;
            //核身未通过
            case 2006:
                String verifyCode = null;
                if (response.extInfo != null) {
                    verifyCode = response.extInfo.get("verifyCode");
                }
                if (TextUtils.equals(verifyCode, NOT_SAME_PERSON)) {
                    //比对非同人
                    msg = "抱歉，没有认出你来，请确保是本人操作";

                } else if (TextUtils.equals(verifyCode, UNABLE_GET_IMAGE)) {
                    //人脸无法获取图片
                    msg = "抱歉，你暂时无法使用刷脸功能";
                } else {
                    //人脸系统异常
                    msg = "人气太旺了，稍候再试试";
                }
                break;
            default:
                msg = "未知错误";
                break;
        }

        final AlertDialog.Builder alertDialogBuilder =
                new AlertDialog.Builder(this);
        alertDialogBuilder.setMessage(msg + "(" + response.code + ")");
        alertDialogBuilder.setPositiveButton(android.R.string.yes,
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
        // 显示
        AlertDialog dialog = alertDialogBuilder.create();
        dialog.show();
    }

    public void onRequestResult(final String zimId) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (TextUtils.isEmpty(zimId)) {
                    Toast.makeText(ZolozFaceTestActivity.this, "zimId为空！无法继续。", Toast.LENGTH_LONG).show();
                } else {
                    ZolozFaceTestActivity.this.zimId = zimId;
                    startZim(ZolozFaceTestActivity.this.zimId);
                }
            }
        });
    }

    private void startZim(String zimId) {
        if (!TextUtils.isEmpty(zimId)) {
            try {
                zimFacade = ZIMFacadeBuilder.create(this);
                zimFacade.verify(zimId, null, this);
            } catch (Throwable e) {
                Log.w(TAG, e);
            }
        }
    }
    private void getMaotaiInfo(){
        //增加从数据库获取数据
        MyThread myThread=new MyThread(this);
        myThread.start();
    }
}
